<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 17:37
 */

namespace app\index\controller;


use think\exception\ErrorException;

class Dynamic extends Common
{
    /**
     * 查询列表 旅说列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function query()
    {
        // $uid = $this->isCheckAuth();
        $uid = input('uid');
        $is = input('is/d');
        if (empty($uid) || !strlen($is)) {
            $this->rest('参数错误', 's004');
        }
        $db = db('Dynamic');
        if($is == 1){ #加精贴 精华
            $db->where('status',1);
        }elseif($is ==2){ # 达人
            $db->where('type',1);
        }elseif($is ==4){
            $follow_id= db('user_follow')->field('follow_id')->where(['uid'=>$uid,'status'=>1])->select();
            $follow_id_arr = array_column($follow_id,'follow_id');
            $db->where('uid','in',$follow_id_arr);
        }
        $page = input('page') > 0 ? input('page/d') : 1;
        $listRows = input('listRows') > 0 ? input('listRows/d') : 10;
        $field = 'id,uid,address,content,images,createtime,trialtime';
        $data = $db->field($field)->where('del',0)->select();
        if ($data) {
            foreach ($data as $k=>$v){
                $data[$k]['images'] =json_decode($v['images'],true);
                $userModel = db('user')->field('id,nickname,headurl')->where('id', $v['uid'])->find();
                if(!empty($userModel)) {
                    $data[$k]['userModel'] = $userModel;
                }
                $islike = db('collection')->where(['uid'=>$uid,'pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->find();
                if(!empty($islike)){
                    $data[$k]['islike'] = 1;
                }else{
                    $data[$k]['islike'] = 0;
                }
                $isshare = db('collection')->where(['uid'=>$uid,'pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'share'])->find();
                if(!empty($isshare)){
                    $data[$k]['isshare'] = 1;
                }else{
                    $data[$k]['isshare'] = 0;
                }
                $isfollow = db('user_follow')->where(['uid'=>$uid,'follow_id'=>$v['uid'],'status'=>'1'])->find();
                if(!empty($isfollow)){
                    $data[$k]['isAttention'] = 1;
                }else{
                    $data[$k]['isAttention'] = 0;
                }
                $comment = db('comment')->where(['pid'=>$v['id'],'modular'=>2,'status'=>0])->count();
                $data[$k]['comment']=$comment;
                $likes = db('collection')->where(['pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->count();
                $data[$k]['likes']=$likes;
                $share = db('collection')->where(['pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'share'])->count();
                $data[$k]['share'] = $share;
                if($v['uid'] != $uid){
                    if(time() <= $v['createtime']+$v['trialtime']){
                        unset($data[$k]);
                    }
                }
            }
            $total = count($data);
            $this->rest(['list' => $data, 'page' => getPage($page, $listRows, $total)]);
        } else {

            $this->rest($data);
        }
    }


    /**
     * 个人旅说  列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        // $uid = $this->isCheckAuth();
        $uid = input('uid'); # 操作用户id
        $dynamic_uid = input('dynamic_uid'); # 旅说作者 id
        if (empty($uid) || empty($dynamic_uid)) {
            $this->rest('参数错误', 's004');
        }
        $db = model('Dynamic')->alias('d');
        $type = input('type/d');
        if ($type) {
            $db->where('type', $type);
        }
        $field = 'd.id,d.uid,u.sex,d.uid as userModel,d.address,d.content,d.images,d.createtime,d.id as commentCount,d.id as likesCount,d.id as shareCount';
        $data = $db->field($field)->join('user u','d.uid=u.id','left')->where('d.uid',$dynamic_uid)->select();
        if ($data) {
            foreach ($data as $k=>$v){
                $islike = db('collection')->where(['uid'=>$uid,'pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->find();
                if(!empty($islike)){
                    $data[$k]['islike'] = 1;
                }else{
                    $data[$k]['islike'] = 0;
                }
                $isshare = db('collection')->where(['uid'=>$uid,'pid'=>$v['id'],'modular'=>'dynamic','status'=>0,'type'=>'share'])->find();
                if(!empty($isshare)){
                    $data[$k]['isshare'] = 1;
                }else{
                    $data[$k]['isshare'] = 0;
                }
                $isfollow = db('user_follow')->where(['uid'=>$uid,'follow_id'=>$v['uid'],'status'=>'1'])->find();
                if(!empty($isfollow)){
                    $data[$k]['isAttention'] = 1;
                }else{
                    $data[$k]['isAttention'] = 0;
                }

            }
            $this->rest($data);
        } else {
            $this->rest('暂无旅说', 's004');
        }
    }

    /**
     * 发布旅说
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        // $uid = $this->isCheckAuth();
        $uid = input('uid');
        $data = array(
            'uid' => $uid,
            'content' => input('content/s'),
            'images' => input('images/a'),
            'address' => input('address/s'),
            'j' => input('j'),
            'w' => input('w'),
        );
        if(empty($uid) || empty($data['content'])){
            $this->rest('参数错误', 's004');
        }
      /*  $validate = validate('Dynamic');
        if (!$validate->check($data)) {
            $this->rest($validate->getError(), 's004');
        }*/
       // $data['images'] = json_encode($data['images']);
        $data['createtime'] = time();
        $data['updatetime'] = time();
        $rest = db('dynamic')->insert($data);
        if ($rest) {
            $this->rest('发布成功');
        } else {
            $this->rest('发布失败');
        }
    }

    /*
     * 查看旅说详情
     */
    public function details()
    {
        //$uid = $this->isCheckAuth();
        $uid = input('uid');
        $did = input('did'); #旅说id
        if (empty($uid) || empty($did)) {
            $this->rest('参数错误', 's004');
        }
        $num = db('dynamic')->where('id',$did)->field('browse_num')->find();
        if (!empty($num)){
            @db('dynamic')->where('id',$did)->update(['browse_num'=>$num['browse_num']+1]);
        }
        $field = 'id,uid,content,images,createtime,browse_num';
        $res = db('dynamic')->field($field)->where(['id'=>$did,'downline'=>0,'del'=>0])->find();
        $userModel = db('user')->field('id,nickname,headurl')->where('id', $res['uid'])->find();
        if(!empty($userModel)) {
            $res['userModel'] = $userModel;
        }
        $comment_db = db('comment')->alias('c');
        $files = 'c.id,c.content,c.like_time,u.id as uid,u.nickname,u.headurl';
        $com = $comment_db->field($files)->join('user u','c.uid=u.id','left')->where(['modular'=>2,'status'=>0,'c.aid'=>$res['id']])->select();
        if(!empty($com)){
            $res['iscomment'] = 1;
        }else{
            $res['iscomment'] = 0;
        }
        $islike = db('collection')->where(['uid'=>$uid,'pid'=>$res['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->find();
        if(!empty($islike)){
            $res['islike'] = 1;
        }else{
            $res['islike'] = 0;
        }
        $isshare = db('collection')->where(['uid'=>$uid,'pid'=>$res['id'],'modular'=>'dynamic','status'=>0,'type'=>'share'])->find();
        if(!empty($isshare)){
            $res['isshare'] = 1;
        }else{
            $res['isshare'] = 0;
        }
        $comment = db('comment')->where(['pid'=>$res['id'],'modular'=>2,'status'=>0])->count();
        $res['comment']=$comment;
        $likes = db('collection')->where(['pid'=>$res['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->count();
        $res['likes']=$likes;
        $share = db('collection')->where(['pid'=>$res['id'],'modular'=>'dynamic','status'=>0,'type'=>'share'])->count();
        $res['share'] = $share;
        $likes_num = db('collection')->field('uid')->where(['pid'=>$res['id'],'modular'=>'dynamic','status'=>0,'type'=>'like'])->select();
        $nickname = db('user')->field('nickname')->where('id','in',array_column($likes_num,'uid'))->select();
        $res['nickname'] = $nickname;
       /* $name = implode('、',array_column($nickname,'nickname'));
        $res['nickname'] = $name;*/
       $this->rest($res);
    }





    /**
     * 发布评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function commentAdd()
    {
        $uid = $this->isCheckAuth();

        //$images = json_decode(input('images/a'), true);
        $images = input('images/a');
        if (empty($images) || !is_array($images) || count($images) <= 0) {
            $this->rest('至少上传一张图片');
        }
        $data = array(
            'uid' => $uid,
            'pid' => input('pid'),
            'modular' => 'dynamic',
            'content' => input('content'),
            'images' => json_encode($images),
        );

        $validate = validate('Comment');

        if (!$validate->check($data)) {

            $this->rest($validate->getError(), 's004');
        }
        $rest = db('comment')->insert($data);
        if ($rest) {
            $this->rest('评论成功');
        } else {
            $this->rest('评论失败');
        }
    }

    /**
     * 删除评论
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function commentDel()
    {
        $uid = $this->isCheckAuth();
        $id = input('id');
        if (empty($id) || empty($uid)) {
            $this->rest('参数错误', 's004');
        }
        $rest = db('comment')->where('id', $id)->where('uid', $uid)->where('modular', 'dynamic')->delete();
        if ($rest) {
            $this->rest('删除成功');
        } else {
            $this->rest('删除失败');
        }
    }

    /**
     * 旅说 添加收藏      1:收藏  2:旅说   did :旅说id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collection()
    {
      //  $uid = $this->isCheckAuth();
        $uid = input('uid');
        $did = input('did');
        $iscollection = input('iscollection');
        if (empty($uid) || empty($did) || empty($iscollection)) {
            $this->rest('参数错误', 's004');
        }
        $data = array(
            'uid' => $uid,
            'pid' => $did,
            'type' => 'collection',
            'modular' => 'dynamic',
        );
        if ($iscollection == 1) {
            $validate = validate('Collection');
            if (!$validate->check($data)) {
                $this->rest($validate->getError(), 's004');
            }
            $query = db('collection')->where(['uid' => $uid, 'pid' => $did, 'type' => 'collection', 'modular' => 'dynamic'])->find();
            if ($query) {
                if ($query['status'] == 1) {
                    $result = db('Collection')->where('id', $query['id'])->update(['status' => 0]);
                    if ($result) {
                        $this->rest('收藏成功');
                    } else {
                        $this->rest('收藏失败', 's004');
                    }
                } else {
                    $this->rest('已收藏,请勿重复收藏', 's004');
                }
            } else {
                $rest = db('collection')->insert($data);
                if ($rest) {
                    $this->rest('收藏成功');
                } else {
                    $this->rest('收藏失败', 's004');
                }
            }
        }else{
            $rest = db('Collection')->where($data)->update(['status' => 1]);
            if ($rest) {
                $this->rest('取消收藏成功');
            } else {
                $this->rest('取消收藏失败', 's004');
            }
        }
    }



    /**
     * 旅说 点赞      1:收藏  2:旅说   did :旅说id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function like()
    {
       // $uid = $this->isCheckAuth();
        $uid = input('uid');
        $did = trim(input('did'));
        $islike = input('islike');
        if(empty($uid) || empty($did) || empty($islike)){
            $this->rest('参数错误', 's004');
        }
        $data = array(
            'uid' => $uid,
            'pid' => $did,
            'type' => 'like',
            'modular' => 'dynamic',
        );
        if($islike == 1){
            $validate = validate('Collection');
            if (!$validate->check($data)) {
                $this->rest($validate->getError(), 's004');
            }
            $query = db('collection')->where(['uid'=>$uid,'pid'=>$did,'type'=>'like','modular'=>'dynamic'])->find();
            if($query){
                if($query['status'] == 1){
                    $result = db('Collection')->where('id', $query['id'])->update(['status'=>0]);
                    if ($result) {
                        $this->rest('点赞成功');
                    } else {
                        $this->rest('点赞失败','s004');
                    }
                }else{
                    $this->rest('已赞过','s004');
                }
            }else{
                $rest = db('collection')->insert($data);
                if ($rest) {
                    $this->rest('点赞成功');
                } else {
                    $this->rest('点赞失败','s004');
                }
            }
        }else{
            $rest = db('Collection')->where($data)->update(['status'=>1]);
            if ($rest){
                $this->rest('取消点赞成功');
            } else {
                $this->rest('取消点赞失败','s004');
            }
        }
    }

    /**
     * 文章  想买,尝鲜    [1: 是, 2:取消]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function wantbuy()
    {
        // $uid = $this->isCheckAuth();
        $uid = input('uid');
        $aid = trim(input('aid'));
        $type = input('type');
        $is = input('is');
        if (empty($uid) || empty($aid) || empty($is) || empty($type)) {
            $this->rest('参数错误', 's004');
        }
        $data = array(
            'uid' => $uid,
            'pid' => $aid,
            'modular' => 'article',
        );
        if($type == 'wantbuy' || $type == 'fresh'){
            $data['type'] = $type;
            if($is == 1){
                $validate = validate('Collection');
                if (!$validate->check($data)) {
                    $this->rest($validate->getError(), 's004');
                }
                $query = db('collection')->where(['uid'=>$uid,'pid'=>$aid,'type'=>$type,'modular'=>'article'])->find();
                if($query){
                    if($query['status'] == 1){
                        $result = db('Collection')->where('id', $query['id'])->update(['status'=>0]);
                        if ($result){
                            $this->rest('操作成功');
                        } else {
                            $this->rest('操作失败','s004');
                        }
                    }else{
                        $this->rest('请勿重复点击','s004');
                    }
                }else{
                    $rest = db('collection')->insert($data);
                    if ($rest) {
                        $this->rest('操作成功');
                    } else {
                        $this->rest('操作失败','s004');
                    }
                }
            }else{
                $rest = db('Collection')->where($data)->update(['status'=>1]);
                if ($rest){
                    $this->rest('取消成功');
                } else {
                    $this->rest('取消失败','s004');
                }
            }
        }
    }

    /*
     * 个人中心--想去看看 || 足迹
     * 1想去看看   2 足迹
     * */
    public function to_see()
    {
        $uid = input('uid');
        $page = input('page') > 0 ? input('page/d') : 1;
        $state = input('state');
        $listRows = input('listRows') > 0 ? input('listRows/d') : 10;
        $type = empty(input('type')) ? false : input('type');
       # p(empty());
        if (empty($uid) || empty($state)) $this->rest('参数错误','s004');
        $get_aid = db('graded')->where(['uid'=>$uid,'state'=>$state])->field('aid')->select();
        $aid = array_column($get_aid,'aid');
        $list = get_a_list($aid,$type,$page,$listRows,2);
        $list['list'] = json_array($list['list']);
        $this->rest(['list' => $list['list'], 'page' => getPage($page, $listRows, $list['total'])]);
    }

    /*
      * 个人中心--足迹
      * */
 /*   public function to_track()
    {
        $uid = input('uid');
        $page = input('page') > 0 ? input('page/d') : 1;
        $listRows = input('listRows') > 0 ? input('listRows/d') : 10;
        $type = empty(input('type')) ? false : input('type');
        if (empty($uid)) $this->rest('参数错误','s004');
        $get_aid = db('graded')->where(['uid'=>$uid,'state'=>2])->field('aid')->select();
        $aid = array_column($get_aid,'aid');
        $list = get_a_list($aid,$type,$page,$listRows);
        $this->rest(['list' => $list['list'], 'page' => getPage($page, $listRows, $list['total'])]);
    }*/

    /*
     * 评论点赞or取消
     * */
    public function comment_like()
    {
        $cid = input('cid');
        $uid = input('uid');
        if (empty($cid) || empty($uid)) $this->rest('参数错误','s004');
        $data = [
            'uid' => $uid,
            'pid' => $cid,
            'modular' => 'comment',
            'status' => 'comment',
            'type' => 'comment',
            'createtime' => time(),
        ];
        $where = ['modular'=>'comment','uid' => $uid,'pid' => $cid];
        $is_like = db('collection')->where($where)->find();
        if (empty($is_like)){
            $insert = db('collection')->insert($data);
            if (empty($insert)) $this->rest('操作失败1');
            $num = db('collection')->where($where)->count();
            db('comment')->where(['id' => $cid])->update(['number'=>$num]);
            $this->rest('点赞成功');
        }else{
            $delect = db('collection')->where($where)->delete();
            if (empty($delect)) $this->rest('操作失败');
            $this->rest('取消点赞');
        }
        $this->rest('操作失败');
    }

}